
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
          * {
            margin: 0;
            padding: 0;
        }

        .register-box {
            margin: 50px auto;
            width: 300px;
        }

        .register-box .title {
            width: 300px;
            line-height: 70px;
            text-align: center;
            font-size: 20px;
            font-family: 'Courier New', Courier, monospace;
        }
        
        .box {
            font-weight: bold;
            line-height: 30px;
            text-align: center;
        }
        
        .register-box .box input {
            height: 40px;
            padding-left: 20px;
            border: 1px solid #ccc;
            background-color: #eee;
            border-radius: 5px;
            width: 100%;
            outline: none;
            margin: 10px 0;
        }
        
        .register-box .zhuce input {
            height: 40px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 320px;
            font-weight: 700;
            outline: none;
            margin: 5px 0;
            background-color: rgb(96, 96, 238);
            font-family: 'Courier New', Courier, monospace;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="register-box">
        <div class="title">注册新账号</div>
        <div class="box">
            <input type="text" placeholder="输入用户名">
        </div>
        <div class="box">
            <input type="password" placeholder="输入密码">
        </div>
        <div class="box">
            <input type="password" placeholder="确认密码">
        </div>
        <div class="box zhuce">
            <input type="button" class="register" value="注册">
        </div>
    </div>
    <script src="./04-ajax.js"></script>
   

</body>

</html>
<script>
    var inputs = document.querySelectorAll('.register-box .box input');
    var select = document.querySelector('.register-box .box select');
    inputs[3].onclick = function () {
        if (inputs[0].value && inputs[1].value && inputs[2]) {
            if (inputs[2].value == inputs[1].value) {
                if (/^\d{4}$/.test(inputs[1].value)) {
                    ajax('get', 'https://yantianfeng.com/api/register', {
                        username: inputs[0].value,
                        password: inputs[1].value,
                        password: inputs[2].value
                    }).then( function (data) {
                        if (data.status != 200) {
                            alert('注册成功');
                        } else {
                            alert('注册失败!');
                        }
                    })
                } else {
                    alert('请输入正确的手机号/密码')
                }
            }else{
                alert('密码和确认密码不相等')
            }
        } else {
            alert('用户名/密码不能为空!')
        }
    }

</script>